<?php

namespace App\Http\Controllers\Admin;

use App\Models\FangAttr;
use Illuminate\Http\Request;


class FangAttrController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //实例化房源属性
        $model = new FangAttr();
        //取数据
        $data = $model->getlist();
        //指定视图并且赋值
        return view('admin.fangattr.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *添加房源属性
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //查询一级节点
        $model = new FangAttr();
        $data = $model->where('pid',0)->get();
        return view('admin.fangattr.create',compact('data'));
    }




    /**
     * Store a newly created resource in storage.
     * 
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        //只有属性名字是必须要进行填写的
        $this->validate($request,[
            'name' => 'required'
        ]);
        //验证通过后，入库并且跳转到列表页面
        //获取数据
        $post = $request->except(['_token','file']);
        //如果传递过来的字段名字不为空的话 就使用字段名字   否则就使用空字符串
        $post['field_name'] = !empty($post['field_name']) ? $post['field_name'] : '';
        FangAttr::create($post);
       return redirect(route('admin.fangattr.index'))->with('success','添加成功');
    }


    //上传icon
    public function upfile(Request $request){
        //上传文章封面
        $pic = config('up.pic');
        //如果确实文件进行文件上传
        if($request->hasFile('file')){
            //将文件进行上传处理
            $ret = $request->file('file')->store('', 'fangattr');
            //获取文件上传的路径
            $pic = '/uploads/fangattr/'.$ret;
        }
        //如果文件上传成功，将路径进行返回
        return ['status'=>0,'url'=>$pic];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\FangAttr  $fangAttr
     * @return \Illuminate\Http\Response
     */
    public function show(FangAttr $fangAttr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     * 
     * @param  \App\Models\FangAttr  $fangAttr
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //查询一级节点
        $model = new FangAttr();
        $data = $model->where('pid', 0)->get();
        $find = FangAttr::find($id);
        return view('admin.fangattr.edit', compact('data','find'));
    }

    /**
     * Update the specified resource in storage.
     * 
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangAttr  $fangAttr
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangAttr $fangAttr,$id)
    {
        try{
            $this->validate($request,[
                'name' => 'required'
            ]);
        }catch(\Exception $e){
            return redirect(route('admin.fangattr.edit',['id'=>$id]))->withErrors(['error'=>'验证失败']);
        }
        //修改数据
        FangAttr::where(['id'=>$id])->update($request->except(['_token','file','_method']));
        return redirect(route('admin.fangattr.edit',['id'=>$id]))->with('success', '操作成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangAttr  $fangAttr
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangAttr $fangAttr,$id)
    {
        FangAttr::destroy($id);
        return ['status' => 0,'msg' => '删除成功'];
    }
}
